﻿using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XiaoYou.Game.Player;

namespace XiaoYou.Game.Script.SnqxExilium.Player
{
    public class StartPlayer : GloPlayer
    {
        public override void Start()
        {
            base.Start();
            EnterAppAndCheck();
            this.SetTimeout(1000 * 60 * 10);
            if (setting.JumpNew)
            {
                Account = null;
                this.SetTimeout(1000 * 60 * 120);
                RegisterAccount();
                JumpNew();
            }
            if (setting.Register)
            {
                this.SetTimeout(1000 * 60 * 120);
                this.GetRegisterAccount();
                if(Account.C2==null || Account.C2=="")
                {
                    RegisterAccount();
                    getEmail();
                }
                else
                {
                    Login();
                }
    
                JumpNew();
            }


            Simulator.Quit();
            Environment.Exit(0);
        }

        /// <summary>
        /// 注册账号
        /// </summary>
        void RegisterAccount()
        {
            Log("注册账号");
            while (lw.FindPicMoveToClick(31, 172, 55, 201, "账号切换1.bmp", 31, 172, 55, 201).Error)
            {
                lw.Delay(2000);
            }

            while (lw.FindPic(569, 369, 598, 399, "login_sanbaologo.bmp").Error)
            {

            }
            lw.MoveToClick(575, 369, 658, 397, 2000);
            while (lw.FindPic(536, 289, 732, 336, "登录按钮1.bmp", "000000", 0.95f, wait: 2000).Error)
            {

            }

            lw.MoveToClick(337, 302, 379, 324, 2000);
            inputBrithDay();
           
            if (Account == null)
            {
                Account = new Share.Data.Account();
                Account.LoginName = Share.CreateAccountAndPassword.GeneratingAccount(6, 10);
                Account.Password = Share.CreateAccountAndPassword.GeneratingPassword(8, 8);
                var accountResult = this.RegisterByEmail(Account.LoginName, Account.Password);
                if (accountResult.Success)
                {
                    Account = accountResult.Data;
                }
                else
                {
                    Environment.Exit(0);
                }
            }
            inputAccountAndPassword();
            Account.C2 = "注册";
            this.updateAccount();

            getEmail();


           

            void inputAccountAndPassword()
            {
                Log("inputAccountAndPassword");
                //判断是否在注册页面，不在就重启
                while (lw.FindPic(219, 164, 329, 269, "log_sanbaobigLogo.bmp", wait: 1000).Error)
                {
                    lw.Delay(1000);
                    Log("等待注册对话框弹出");
                }
                InputUserNameAndPassword();
                LogAccount();
                //同意选择框
                lw.MoveToClick(342, 351, 357, 369, 1000);

                //注册按钮
                lw.MoveToClick(399, 296, 689, 329, 1000);

            }


            //输入出生日期
            void inputBrithDay()
            {
                lw.MoveToClick(310, 233, 614, 258, 2000);
                lw.SendString(GenerateBirthDate(), Mod.m2, wait: 2000);
                lw.MoveToClick(544, 317, 604, 342);
            }
        }

        bool getEmail()
        {
            Log("getEmail");
            var getEmailResult = this.GetEmail("sunborn", 20);
            if (getEmailResult.Data.Contains("table"))
            {
                var emailContent = $"<html><body>{getEmailResult.Msg}</body></html>";

                HtmlAgilityPack.HtmlDocument document = new HtmlDocument();
                document.LoadHtml(emailContent);
                var span = document.DocumentNode.SelectSingleNode("/html/body/table/tbody/tr/td/p[1]/span[2]");
                if (span != null)
                {
                    var result = new HttpClient().GetStringAsync(span.InnerHtml).Result;
                    if (result.Contains("login"))
                    {
                        lw.MoveToClick(523, 356, 681, 403);
                        return true;
                    }
                }
            }
            return false;

        }

        public static string GenerateBirthDate()
        {
            // 计算年龄范围对应的日期区间（以当前日期为基准）
            DateTime today = DateTime.Today;

            // 最大出生日期（30岁边界：今天的日期减去30年）
            DateTime maxBirthDate = today.AddYears(-19);

            // 最小出生日期（19岁边界：今天的日期减去31年，加1天，确保满19岁）
            DateTime minBirthDate = today.AddYears(-31).AddDays(1);

            // 生成随机日期
            Random random = new Random();
            int daysRange = (maxBirthDate - minBirthDate).Days;
            DateTime randomBirthDate = minBirthDate.AddDays(random.Next(daysRange + 1));

            // 转换为yyyyMMdd格式
            return randomBirthDate.ToString("yyyyMMdd");
        }

        /// <summary>
        /// 新手任务
        /// </summary>
        void JumpNew()
        {
            var runCount = 0;
            var watiTime = 20;
            Stopwatch sw = Stopwatch.StartNew();
            sw.Start();
            while (true && runCount < 10)
            {
                Log(sw.Elapsed.TotalSeconds.ToString());
                if (sw.Elapsed.TotalMinutes > watiTime)
                {
                    runCount++;
                    base.Start();
                    this.EnterAppAndCheck();
                    lw.Delay(2000);
                    lw.MoveToClick(434, 447, 527, 464, 1000 * 10);
                    sw.Restart();
                }

                lw.Delay(2000);
                //1.action1 
                lw.FindPicMoveToClick(459, 170, 567, 189, "action1.bmp", 452, 255, 500, 283, wait: 500);
                lw.FindPicMoveToClick(351, 318, 483, 346, "action1.bmp", 459, 250, 500, 287, wait: 500);
                lw.FindPicMoveToClick(42, 219, 175, 243, "action1.bmp", 459, 250, 500, 287, wait: 500);
                lw.FindPicMoveToClick(423, 166, 535, 184, "action1.bmp", 459, 250, 500, 287, wait: 500);
                lw.FindPicMoveToClick(290, 311, 329, 336, "action2.bmp", 459, 250, 500, 287, wait: 500);
                lw.FindPicMoveToClick(509, 274, 637, 295, "action3.bmp", 452, 243, 495, 280, wait: 500);
                //

                lw.FindPicMoveToClick(415, 147, 536, 167, "action02_1.bmp", 451, 240, 502, 294, wait: 500);
                lw.FindPicMoveToClick(414, 152, 538, 174, "action02_1_1.bmp", 454, 171, 480, 194, wait: 500);
                lw.FindPicMoveToClick(323, 260, 446, 280, "action02_1_2.bmp", 454, 171, 480, 194, wait: 500);
                lw.FindPicMoveToClick(412, 102, 538, 121, "action02_18.bmp", 412, 20, 436, 45, wait: 500);
                lw.FindPicMoveToClick(306, 224, 429, 241, "action02_18.bmp", 454, 171, 480, 194, wait: 500);
                lw.FindPicMoveToClick(337, 330, 377, 353, "action02_2.bmp", 454, 253, 503, 288, wait: 500);
                lw.FindPicMoveToClick(858, 447, 929, 527, "action02_3.bmp", 858, 447, 929, 527, wait: 500);
                lw.FindPicMoveToClick(858, 447, 929, 527, "action02_3.bmp", 459, 250, 500, 287, wait: 500);
                lw.FindPicMoveToClick(437, 170, 544, 189, "action02_4.bmp", 451, 249, 504, 286, wait: 500);
                lw.FindPicMoveToClick(431, 373, 539, 388, "action02_5.bmp", 451, 249, 504, 286, wait: 500);
                lw.FindPicMoveToClick(360, 221, 464, 235, "action02_6.bmp", 387, 299, 434, 332, wait: 500, sim: 0.85f);
                lw.FindPicMoveToClick(426, 167, 532, 181, "action02_12.bmp", 387, 299, 434, 332, wait: 500);
                lw.FindPicMoveToClick(426, 167, 532, 181, "action02_12.bmp", 455, 48, 484, 68, wait: 500);
                //低于地方攻击2回合 0/2
                lw.FindPicMoveToClick(441, 304, 547, 320, "action02_13.bmp", 479, 369, 511, 399, wait: 2000);

                lw.FindPicMoveToClick(429, 189, 538, 204, "action02_21.bmp", 466, 256, 497, 293, wait: 2000);
                lw.FindPicMoveToClick(402, 226, 427, 250, "action02_14.bmp", 198, 282, 242, 320, wait: 2000);

                //低于地方攻击2回合 1/2
                lw.FindPicMoveToClick(560, 180, 666, 197, "action02_13.bmp", 581, 245, 620, 289, sim: 0.80f, wait: 2000);

                lw.FindPicMoveToClick(429, 189, 538, 204, "action02_21.bmp", 466, 256, 497, 293, wait: 2000);
                lw.FindPicMoveToClick(521, 149, 550, 182, "action02_14.bmp", 89, 107, 157, 152, wait: 2000);

                lw.FindPicMoveToClick(560, 180, 666, 197, "action02_13.bmp", 581, 245, 620, 289, sim: 0.80f, wait: 2000);

                lw.FindPicMoveToClick(429, 189, 538, 204, "action02_21.bmp", 466, 256, 497, 293, wait: 2000);
                lw.FindPicMoveToClick(521, 149, 550, 182, "action02_14.bmp", 59, 54, 60, 54, wait: 2000);

                lw.FindPicMoveToClick(560, 180, 666, 197, "action02_13.bmp", 581, 245, 620, 289, sim: 0.80f, wait: 2000);

                lw.FindPicMoveToClick(423, 166, 535, 184, "action1.bmp", 459, 250, 500, 287, wait: 500);
                lw.FindPicMoveToClick(429, 189, 538, 204, "action02_21.bmp", 466, 256, 497, 293, wait: 2000);
                lw.FindPicMoveToClick(389, 157, 420, 188, "action02_14.bmp", 135, 98, 157, 118, wait: 2000);

                lw.FindPicMoveToClick(423, 166, 535, 184, "action1.bmp", 459, 250, 500, 287, wait: 500);
                lw.FindPicMoveToClick(429, 189, 538, 204, "action02_21.bmp", 466, 256, 497, 293, wait: 2000);
                lw.FindPicMoveToClick(389, 157, 420, 188, "action02_14.bmp", 9, 115, 45, 146, wait: 2000);

                lw.FindPicMoveToClick(401, 157, 426, 181, "action02_20.bmp", 464, 236, 489, 265, wait: 2000);

                lw.FindPicMoveToClick(423, 166, 535, 184, "action1.bmp", 459, 250, 500, 287, wait: 500);
                lw.FindPicMoveToClick(429, 189, 538, 204, "action02_21.bmp", 466, 256, 497, 293, wait: 2000);
                lw.FindPicMoveToClick(389, 157, 420, 188, "action02_14.bmp", 9, 115, 45, 146, wait: 2000);
                //438,251,546,270

                lw.FindPicMoveToClick(423, 166, 535, 184, "action1.bmp", 459, 250, 500, 287, wait: 500);
                lw.FindPicMoveToClick(438, 251, 546, 270, "action02_21.bmp", 479, 318, 506, 346, wait: 2000);
                lw.FindPicMoveToClick(389, 157, 420, 188, "action02_14.bmp", 9, 115, 45, 146, wait: 2000);

                //低于地方攻击2回合 2/3
                lw.FindPicMoveToClick(445, 42, 469, 65, "action02_22.bmp", 459, 41, 482, 60, wait: 2000);


                lw.FindPicMoveToClick(361, 38, 495, 58, "action02_21.bmp", 408, 16, 428, 32, wait: 2000);
                //
                lw.FindPicMoveToClick(325, 28, 364, 69, "action02_22.bmp", 457, 55, 485, 77, wait: 2000);
                if (lw.FindPic(529, 186, 620, 272, "action02_19_*.bmp").Success)
                {
                    lw.MoveToClick(477, 316, 510, 349, 2000);
                    lw.MoveToClick(109, 120, 130, 134, 2000);
                }


                if (lw.FindPic(139, 99, 218, 120, "action02_15.bmp").Success)
                {
                    lw.FindPicMoveToClick(803, 378, 832, 414, "action02_16.bmp", 808, 382, 903, 405, wait: 2000);

                }
                lw.FindPicMoveToClick(721, 436, 920, 477, "action02_17.bmp", 721, 436, 920, 477);

                lw.FindPicMoveToClick(665, 466, 847, 495, "确认按钮.bmp", 665, 466, 847, 495);


                lw.FindPicMoveToClick(1, 1, 551, 532, "new对话框*.bmp", cx1: 100, cy1: 200);
                lw.FindPicMoveToClick(712, 8, 923, 45, "new下一步*.bmp", cx1: 916, cy1: 24, wait: 2000);
                lw.FindPicMoveToClick(866, 17, 933, 38, "new下一步4.bmp", 58, 24, 63, 30, wait: 500);
                lw.FindPicMoveToClick(777, 365, 934, 428, "new确认*.bmp", cx1: 777, cy1: 365);
                lw.FindPicMoveToClick(791, 377, 922, 416, "new确认*.bmp", 795, 383, 110, 30);
                lw.FindPicMoveToClick(484, 368, 711, 445, "new跳过剧情*.bmp", 509, 389, wait: 2000);
                lw.FindPicMoveToClick(32, 101, 924, 523, "点击标记*.bmp");
                //if (lw.FindPicMoveToClick(1, 1, 960, 540, "new角色选择框*.bmp", sim: 0.85f).Success)
                //{
                //    lw.MoveToClick(lw.GetX + 20, lw.GetY);
                //    lw.MoveToClick(lw.GetX - 20, lw.GetY);
                //}
                if (lw.FindPicMoveToClick(837, 100, 891, 151, "角色昵称界面*.bmp", 837, 100, 891, 151).Success)
                {
                    lw.MoveToClick(619, 451, 819, 481, 2000);
                }
                if (lw.FindPic(489, 273, 522, 298, "new选择怪物*.bmp").Success || lw.FindPic(298, 70, 515, 130, "new选择怪物*.bmp").Success)
                {
                    lw.MoveToClick(452, 254, 502, 285);
                }

                lw.FindPicMoveToClick(899, 14, 934, 53, "新手_右上角叉.bmp", 899, 14, 934, 53, sim: 0.85f);

                while (lw.FindPic(31, 90, 228, 128, "new击杀敌方单位1-2*bmp").Success)
                {
                    lw.MoveToClick(622, 26, 644, 53, 1000);
                }
                //lw.MoveToClick(452, 254, 502, 285);
                Log("JumpNew");
                if (lw.FindPic(398, 179, 556, 292, "endnew1.bmp").Success)
                {
                    Account.C2 = "新手结束";
                    Account.C3 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    this.updateAccount();
                    lw.Delay(2000);
                    break;
                }
            }
        }
    }
}
